﻿Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Nodes
Imports DevExpress.XtraEditors.Repository

Public Class FormSettingMenu
    Dim arrMenuItem() As ToolStripMenuItem
    Dim roleid As String
    Private Sub CreateMenu()
        Dim dtmenu As New DataTable
        If TreeMenu.Nodes.Count > 0 Then
            TreeMenu.Nodes.Clear()
        End If
        If Utils.executeSP("proc_z_getrolemenu", New Object() {"all", 0}, dtmenu) Then
            If dtmenu.Rows.Count > 0 Then
                For Each row As DataRow In dtmenu.Rows
                    If row("header") = "0" Then
                        'If row("namaform") <> "Separator" Then
                        Dim node As New TreeNode
                        node.Text = row("menuname")
                        node.Tag = row("formname")
                        node.Name = row("menuid")
                        TreeMenu.Nodes.Add(node)
                        'End If
                    Else
                        Dim nodes() As TreeNode
                        nodes = TreeMenu.Nodes.Find(row("header"), True)
                        'MessageBox.Show(nodes.Count.ToString)
                        If nodes.Count() = 1 Then
                            'If row("namaform") <> "Separator" Then
                            Dim node As New TreeNode
                            node.Text = row("menuname")
                            node.Tag = row("formname")
                            node.Name = row("menuid")
                            nodes(0).Nodes.Add(node)
                            'End If
                        End If
                    End If
                Next
            End If
        End If

        'TreeMenu.ExpandAll()
        TreeMenu.CollapseAll()
        TreeMenu.Width = 200
    End Sub

    Private Sub GetMenuItems(ByVal menu As ToolStripMenuItem, ByRef masternode As TreeNode)
        'MessageBox.Show(menu.DropDownItems.Count)
        If menu.DropDownItems.Count > 0 Then
            For Each item As ToolStripMenuItem In menu.DropDownItems
                Dim node As New TreeNode
                node.Text = item.Text
                node.Name = "Node" & item.Name
                GetMenuItems(item, node)
                masternode.Nodes.Add(node)
            Next
        End If
    End Sub
    Private Sub SetMenuItem(ByVal roleid, ByRef node)
        If node.Nodes.Count > 0 Then
            For Each childnode As TreeNode In node.Nodes
                SetMenuItem(roleid, childnode)
                If childnode.Checked = True Then
                    If Utils.exec_SP("proc_z_setrolemenu", New Object() {roleid, childnode.Name}) = False Then
                        MessageBox.Show("Simpan menu gagal.")
                    End If
                End If
            Next
        End If
    End Sub

    Private Sub UncheckNode(ByRef node)
        If node.Nodes.Count > 0 Then
            For Each childnode As TreeNode In node.Nodes
                UncheckNode(childnode)
                childnode.Checked = False
            Next
        End If
    End Sub
    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        Dim dlguser As New DialogUser
        dlguser.ShowDialog()
    End Sub

    Private Sub SimpleButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton3.Click
        Dim dlgrole As New DialogRole
        dlgrole.ShowDialog()
        Dim dtrole As New DataTable
        If Utils.executeSP("proc_z_getrole", New Object() {0, ""}, dtrole) Then
            If dtrole IsNot Nothing Then
                GridControl1.DataSource = dtrole
            End If
        End If
    End Sub

    Private Sub FormSettingMenu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dtrole As New DataTable
        If Utils.executeSP("proc_z_getrole", New Object() {0, ""}, dtrole) Then
            If dtrole IsNot Nothing Then
                GridControl1.DataSource = dtrole
            End If
        End If
        'TreeMenu.CheckBoxes = True
        CreateMenu()
        TreeMenu.ExpandAll()
    End Sub


    Private Sub TreeMenu_CustomNodeCellEdit(ByVal sender As System.Object, ByVal e As DevExpress.XtraTreeList.GetCustomNodeCellEditEventArgs)
        'If e.Column.FieldName = "colnama" Then
        '    Dim chkedit As RepositoryItemCheckEdit
        '    chkedit = e.RepositoryItem
        '    'chkedit.GlyphAlignment = DevExpress.Utils.HorzAlignment.Near
        '    Dim str As String
        '    str = e.Node.GetValue("colnama").ToString
        '    If str = "True" Then
        '        Exit Sub
        '    ElseIf str = "False" Then
        '        Exit Sub
        '    End If
        '    If chkedit.Caption = String.Empty Then
        '        chkedit.Caption = chkedit.Name
        '    End If

        '    'e.RepositoryItem = chkedit
        '    'End If


        'End If
    End Sub

    Private Sub TreeMenu_FocusedNodeChanged(ByVal sender As System.Object, ByVal e As DevExpress.XtraTreeList.FocusedNodeChangedEventArgs)

    End Sub

    Private Sub GridView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView1.Click
        If GridView1.FocusedRowHandle > -1 Then
            Dim i As Integer = GridView1.FocusedRowHandle
            For Each nodes As TreeNode In TreeMenu.Nodes
                If nodes.Nodes.Count > 0 Then
                    UncheckNode(nodes)
                End If
                nodes.Checked = False
            Next
            Dim dtmenu As New DataTable
            Dim dr As DataRow
            dr = GridView1.GetDataRow(i)
            If dr Is Nothing Then
                Exit Sub
            End If
            roleid = dr(0).ToString
            txtrole.Text = dr(1).ToString
            If Utils.executeSP("proc_z_getrolemenu", New Object() {"role", roleid}, dtmenu) Then
                If dtmenu IsNot Nothing Then
                    If dtmenu.Rows(0).Item(0).ToString = "NO_DATA_FOUND TO SELECT" Then
                        MessageBox.Show("NO_DATA_FOUND TO SELECT")
                    Else
                        For Each row As DataRow In dtmenu.Rows
                            Dim nodes() As TreeNode
                            nodes = TreeMenu.Nodes.Find(row.Item("menuid").ToString(), True)
                            If nodes.Count = 1 Then
                                TreeMenu.SelectedNode = nodes(0)
                                TreeMenu.SelectedNode.Checked = True
                                TreeMenu.Refresh()
                            End If
                        Next
                    End If

                End If
            End If
        End If
    End Sub

    Private Sub SimpleButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton4.Click
        For Each nodes As TreeNode In TreeMenu.Nodes
            If nodes.Nodes.Count > 0 Then
                UncheckNode(nodes)
            End If
            nodes.Checked = False
        Next
        Dim dtrole As New DataTable
        If Utils.executeSP("proc_z_getrole", New Object() {0, ""}, dtrole) Then
            If dtrole IsNot Nothing Then
                GridControl1.DataSource = dtrole
            End If
        End If
    End Sub
    Private Sub SimpanRoleMenu()
        Me.Cursor = Cursors.WaitCursor
        'If exec_SP("proc_z_setrole", New Object() {"edit", roleid, TextRole.Text}) Then
        If Utils.exec_SP("proc_z_deleterolemenu", New Object() {roleid}) Then

        End If
        For Each nodes As TreeNode In TreeMenu.Nodes
            If nodes.Nodes.Count > 0 Then
                SetMenuItem(roleid, nodes)
            End If
            If nodes.Checked = True Then
                If Utils.exec_SP("proc_z_setrolemenu", New Object() {roleid, nodes.Name}) = False Then
                    MessageBox.Show("Simpan menu gagal.")
                    Return
                End If
            End If
        Next
        'End If
        ' End If
        Me.Cursor = Cursors.Default
        MessageBox.Show("Data Berhasil disimpan.")
        Dim dtrole As New DataTable
        If Utils.executeSP("proc_z_getrole", New Object() {0, ""}, dtrole) Then
            If dtrole IsNot Nothing Then
                GridControl1.DataSource = dtrole
            End If
        End If
    End Sub
    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton2.Click
        Dim retval As Boolean = False
        For Each node As TreeNode In TreeMenu.Nodes
            If node.Nodes.Count > 0 Then
                For Each nnode As TreeNode In node.Nodes
                    If nnode.Checked Then
                        retval = True
                        Exit For
                    End If
                Next
            End If
            If node.Checked Then
                retval = True
                Exit For
            Else
                retval = False
            End If
        Next
        If Not retval Then
            MsgBox("Pilihan Menu Tidak Valid", MsgBoxStyle.Exclamation)
            Exit Sub
        End If


        SimpanRoleMenu()
        For Each nodes As TreeNode In TreeMenu.Nodes
            If nodes.Nodes.Count > 0 Then
                UncheckNode(nodes)
            End If
            nodes.Checked = False
        Next
    End Sub

    Private Sub TreeMenu_AfterCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeMenu.AfterCheck
        'If e.Node.Nodes.Count > 0 Then
        '    For Each nod As TreeNode In e.Node.Nodes
        '        If e.Node.Checked Then
        '            nod.Checked = True
        '        Else
        '            nod.Checked = False
        '        End If

        '    Next
        'End If
    End Sub

    Private Sub SimpleButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton5.Click
        If roleid = String.Empty Then
            MsgBox("Role Belum Dipilih !", MsgBoxStyle.Exclamation)
            Exit Sub
        End If

        Dim dlg As New RoleExecute
        dlg.Setroleid = CInt(roleid)
        dlg.ShowDialog()
    End Sub
End Class
